//
// perc2 - calculation of the fractal dimension of correlated
// bond percolation cluster hulls
//
// Copyright (C) 2009, 2010 Indrek Mandre <indrek(at)mare.ee>
// http://www.mare.ee/indrek/perc2/, http://code.google.com/p/perc2/
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
// 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include <map>
#include <vector>

static void distaggr (int argc, char *argv[])
{
 typedef unsigned long long big_t;
 typedef std::vector<long double> vec_t;
 typedef std::map<double, vec_t > data_t;

 data_t data;

  for ( int i = 1; i < argc; i++ )
    {
      FILE *fp = fopen (argv[i], "r");
      if ( !fp )
        {
          fprintf (stderr, "Can't open '%s'\n", argv[i]);
          exit(-1);
        }

      while (1)
        {
          char line[4096];
          if ( !fgets (line, sizeof(line), fp) )
              break;
          if ( !isdigit (line[0]) && line[0] != '-' )
              continue;

          std::istringstream stream(line);

          double a, b;
          //stream >> a >> c >> b;
          stream >> a >> b;

          data_t::iterator fit = data.find (a);
          bool first = false;
          if ( fit == data.end() )
            {
              first = true;
              fit = data.insert (std::make_pair (a, vec_t())).first;
              fit->second.push_back (0);
            }

          vec_t& vec = fit->second;
          vec[0] += b;
        }
      fclose (fp);
    }

  for ( data_t::iterator it = data.begin(); it != data.end(); it++ )
    {
      double key = it->first;
      double val = it->second[0];
      printf ("%g %g\n", key, val);
    }
}

int main (int argc, char *argv[])
{
  typedef unsigned long long big_t;
  typedef std::vector<long double> vec_t;
  typedef std::map<double, vec_t > data_t;
  size_t M = 0;
  data_t data;

  bool checking = true;

  for ( int i = 1; i < argc; i++ )
    {
      FILE *fp = fopen (argv[i], "r");
      if ( !fp )
        {
          fprintf (stderr, "Can't open '%s'\n", argv[i]);
          exit(-1);
        }

      size_t N = 0;

      while (1)
        {
          char line[4096];
          if ( !fgets (line, sizeof(line), fp) )
              break;
          if ( !isdigit (line[0]) )
              continue;

          if ( checking )
            {
              if ( strchr (line, ' ') )
                {
                  fclose (fp);
                  distaggr (argc, argv);
                  exit(0);
                }
              checking = false;
            }
          
          std::istringstream stream(line);

          double ad;
          size_t m;
          char c;
          stream >> ad >> c >> m;

          if ( !M ) M = m;
          if ( m != M || !m )
            {
              fprintf (stderr, "Inconsistent data files!\n");
              exit(-1);
            }

          data_t::iterator fit = data.find (ad);
          bool first = false;
          if ( fit == data.end() )
            {
              first = true;
              fit = data.insert (std::make_pair (ad, vec_t())).first;
              fit->second.push_back (0);
            }

          vec_t& vec = fit->second;

          size_t k = 1;
          for ( size_t i = 0; i < 1 + m + m * (m + 1) / 2; i++ )
            {
              long double val;
              stream >> c >> val;
              if ( first )
                {
                  vec.push_back (val);
                }
              else
                {
                  if ( k >= vec.size() )
                    {
                      fprintf (stderr, "Inconsistent data files!\n");
                      exit(-1);
                    }
                  vec[k++] += val;
                }
              if ( i == 0 )
                  vec[0] += val * ad;
            }

          N++;
        }
      fclose (fp);
    }

  for ( data_t::iterator it = data.begin(); it != data.end(); it++ )
    {
      vec_t& vec = it->second;
      //printf ("%zu,%zu", it->first, M);
      printf ("%.4Lf,%zu", vec[0] / vec[1], M);
      for ( size_t i = 1; i < vec.size(); i++ )
          printf (",%.12Le", vec[i]);
      printf ("\n");
    }
}

